<?php

use Net\Connection;
use Net\Event;
use Http\HttpConnection;

ini_set('display_errors', 1);
error_reporting(E_ALL ^ E_NOTICE);

require_once "Http/HttpConnection.php";

class HttpProxy {
    
    public function __construct()
    {
        $httpserver = new Connection();
        $httpserver->setBlock(true);
        $httpserver->connect('192.168.88.128', 1081);
        
        echo "begin:";var_dump($httpserver->getpeername());
        $httpserver->shutdown(Connection::SHUTDOWN_WRITE);
        echo "shutdown:";var_dump($httpserver->getpeername());
        echo "avali:";var_dump($httpserver->available());
    
        $httpserver->onError(function(Event $e) {
            print_r($e->param());
        });
        
        echo "send:";var_dump($httpserver->send('123')->getLastReturn());
        
        
        // if($httpserver->getLastError()) {
        //     echo "连接失败!\n";
        //     die;
        // }
        //
        // var_dump($httpserver->available());
        //
        // $httpserver->close();
        //
        // var_dump($httpserver->getpeername());
        
        // $ip = '127.0.0.1';
        // $port = '55555';
        // $ip = '192.168.68.155';
        // $port = '55555';
        // $ip = '172.217.24.74';
        // $port = '443';
        
        // $httpserver = new Connection();
        // $httpserver->listening('0.0.0.0', 55557);
        //
        // $httpserver->onNewConnect(function(\Net\Event $e) {
        //     $conn = $e->getConnection();
        //
        //     $conn->onRecv(function(\Net\Event $e) {
        //         $content = $e->getMessage();
        //
        //         echo $content . "\n";
        //     });
        // });
        
        // $httpClient = new HttpConnection();
        // $ary = [
        //     'GET http://www.baidu.com/ HTTP/1.1',
        //     'Host: www.baidu.com',
        //     'User-Agent: curl/7.55.1',
        //     'Accept: */*',
        //     'Proxy-Connection: Keep-Alive',
        //     "\r\n"
        // ];
        //
        // $head = implode("\r\n", $ary);
        // $info = HttpConnection::parseRequest($head);
        // print_r($info);
        // echo HttpConnection::buildRequest($info);
        // die;
        //
        // $httpClient->onConnect(function() use ($httpClient) {
        //     $ary = [
        //         'GET http://www.baidu.com/ HTTP/1.1',
        //         'Host: www.baidu.com',
        //         'User-Agent: curl/7.55.1',
        //         'Accept: */*',
        //         'Proxy-Connection: Keep-Alive',
        //         "\r\n"
        //     ];
        //
        //     $head = implode("\r\n", $ary);
        //
        //     echo '';
        //     echo "send ===============\n$head";
        //     $httpClient->send($head);
        //     echo "====================\n";
        // });
        //
        // $httpClient->onRecv(function(Event $e) {
        //     echo "recv ===============\n{$e->getMessage()}";
        //     echo "====================\n";
        // });
        //
        // $httpClient->onConnectFail(function() {
        //     echo "connect fail!\n";
        // });
        //
        // $httpClient->connect('192.168.68.155', 8888);
        //
        // Connection::dispatch();
    }
}

new HttpProxy;
